import superagent from "superagent";
import fs from "fs";
import path from "path";
import WillesAnalyzer from "./analyzer";
interface Analyzer {
  analyze: (html: string, filePath: string) => string;
}
class Crowller {
  private rawHtml = "";
  private filePath = path.resolve(__dirname, "../data/data.json");
  // 获取dom
  private async getRawHtml() {
    const res = await superagent.get(this.url);
    return res.text;
  }
  // 初始化调用
  private async initSpiderProcess() {
    const html = await this.getRawHtml();
    const content = this.analyzer.analyze(html, this.filePath);
    this.writeFile(content);
  }
  // 写入文件
  private writeFile(content: string) {
    fs.writeFileSync(this.filePath, content);
  }
  constructor(private url: string, private analyzer: Analyzer) {
    this.initSpiderProcess();
  }
}

const url = "https://www.ximalaya.com/toutiao/updates/";
const analyzer = WillesAnalyzer.getInstance();
new Crowller(url, analyzer);
